<?php
class HtmlController extends AppController
{
   var  $name='Html';
   var $view='Theme';
   var $theme='default';
   var $uses = array('Type','Site','Ad','User','Rec','Favorites');
   var $helpers=array('javascript','ajax','html','form');
   var $layout='yongs';
   var $autoRender=false;
   var $tplDebug ;
   var $components=array('Auth'); 
    function beforeFilter()
    {
        $this->tplDebug = Configure::read('tplDebug');
        $this->Auth->allow('*');
        $this->pageTitle='勇士导航网站CMS系统';
    }
    
    //获取模型的数据视图
    function getDateView($modelName,$dvName,$param)
    {
          if(!isset($this->params['requested']))
                $this->redirect('page404','404');
        $dvName='dv_'.$dvName;
        $this->loadModel($modelName);
        if(!isset($this -> $modelName) and is_callable($this->$modelName->$dvName))
            $result=$this->$modelName->$dvName($param);
        else
            $result=array('error'=>'无非获取视图数据,请检查标签参数和名字是否书写错误');
        
        if(array_key_exists('error',$result)){
            if($this->tplDebug)
                return $result;
            else
                return array();
        }
        return $result;
    }
    
    //获取当前登录用户信息
    function getUser()
    {
        if(!isset($this->params['requested']))
                $this->redirect('page404','404');
        return $this->Auth->user();
    }
    
    //方法，页面渲染
    function typepage($tid)
    {
        if(!is_numeric($tid) or !$this->Type->findById($tid)){
           if(isset($this->params['requested'])){
                echo "error:参数tid{$tid}指定的分类并不存在";
                return;
            }else{
                $this->redirect('page404','404');
            } 
         }
        $sites=$this->dvGetSites($tid);
        $this->set('sites',$sites);   
        $typeinfo=$this->Type->find('first',array('conditions'=>array('id'=>$tid),
        'fields'=>array('id','name','keyword','description','tplName')));
        if(empty($typeinfo)){
            if($isStatic)
                echo "error:参数tid{$tid}指定的分类并不存在";
            else
                $this->redirect('page404','404');
        }
        $typeinfo=$typeinfo['Type'];
        $childinfo=$this->Type->Children($tid,true,array('id','name','hide'),'pri');
        foreach($childinfo as $k=>$v)
            $childinfo[$k]=$v['Type'];
        $this->set('child',$childinfo);
        $this->set('keyword',$typeinfo['keyword']);
        unset($typeinfo['keyword']);
        $this->set('description',$typeinfo['description']);
        unset($typeinfo['description']);
         $tpl=$typeinfo['tplName'];
        unset($typeinfo['tplName']);
        $this->set('type',$typeinfo);
        $outHtml=$this->render(null,'page',$tpl);
        if(isset($this->params['requested']))
            return $outHtml;
    }
    
    //方法，生成自定义页面渲染
    function customPage($tplname)
    {
        if(file_exists('..'.DS.'views'.DS.'html'.DS.$tplname.'.ctp')){
            $outHtml=$this->render(null,null,$tplname);
            return $outHtml;
        }else{
            if(isset($this->params['requested'])){
                return "error:自定义页面模板{$tplname}并不存在";
            }else{
                $this->redirect('page404','404');
             }
        }
    }
    
    function page404()
    {
        $this->render(null,null,'custom_page404');
    }
   

    
    //首页控制器
    function index()
    {
        $toptype=$this->Type->find('all',array('conditions'=>array('parent_id'=>NULL),
        'fields'=>array('id','name','htmlPath'),'order'=>'pri'));
        foreach($toptype as $k => $v){
            $toptype[$k]=$v['Type'];
        }
        $this->set('toptype',$toptype);
        $outHtml=$this->render();
        if(isset($this->params['requested']))
        {
            return $outHtml;
        }
    }
  
    //用户注册和登录
     function  signUp()
    {
        if(!empty($this->data)){
            if(isset($this->data['User']['password2']))
                $this->data['User']['password2Hash']=$this->Auth->password(
                $this->data['User']['password2']);
         if($this->User->save($this->data)){
               $this->Auth->login();
               $this->flash('注册成功','/');
          }
        }
    }
    
    function login()
    {
         $this->autoRender=true;
    }
    
    function logout()
    {
        $this->redirect($this->Auth->logout());
    }
    
    //js调用元素
    function jscall($en,$paraArr)
    {
        $params=explode('|',$paraArr);
        $this->set('elename',$en);
        $this->set('parr',$paraArr);
    }
}
?>